<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\CarBlacklistDao;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder as Form;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Route as Url;

class CarBlacklistServices extends BaseServices
{
    public function __construct(CarBlacklistDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [], bool $is_page = false){
        $page = $limit = 0;
        if($is_page) [$page,$limit] = $this->getPageValue();
        $list = $this->dao->getList($where,$page,$limit,$is_page);
        return $list;
    }

    /**
     * 添加
     * @param int $id
     * @return array
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(int $id){
        $info = $this->dao->get($id);
        $field = array();
        $title = $id ? '修改' : '添加';
        $field[] = Form::hidden('id', $info['id']??0);
        $field[] = Form::input('phone', '电话号码', $info['phone']??'');
        $field[] = Form::input('card_id','身份证号',$info['card_id']??'');
        $field[] = Form::textarea('remark','备注',$info['remark']??'');
        return create_form($title, $field, Url::buildUrl('/car/blacklist/save'), 'POST');
    }

    /**
     * 保存
     * @param int $id
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(int $id, array $data){
        if ($id) {
            if (!$this->dao->get($id)) throw new AdminException('数据不存在');
            if ($this->dao->update($id, $data)) throw new AdminException('修改失败或者您没有修改什么');
        } else {
            unset($data['id']);
            $data['add_time'] = time();
            if (!$this->dao->save($data)) throw new AdminException('添加失败');
        }
        return true;
    }

    /**
     * 删除
     * @param int $id
     * @return string
     */
    public function delete(int $id){
        $res = $this->dao->update($id,['is_del'=>1,'delete_time'=>time()]);
        if(!$res) throw new AdminException('删除失败,请稍候再试');
        return '删除成功';
    }

    /**
     * 黑名单验证
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function verificationBlacklist(array $data){
        if(isset($data['uid'])&&$data['uid']){
            $user = Db::name('user')->find($data['uid']);
            if(!$user['status']) throw new ValidateException('黑名单用户，禁止操作');
        }
        if((isset($data['verification']))&&$data['verification']){
            $info = Db::name('car_blacklist')->where('phone|card_id','=',$data['verification'])->where('is_del',0)->find();
            if($info) throw new ValidateException('黑名单用户，禁止操作');
        }
        return true;
    }
}
